
layui.use(['element','jquery','form','table','layer','laydate','layedit'],function () {
    var element = layui.element;
    var $=layui.jquery;
    var form=layui.form;
    var table=layui.table;
    var layer=layui.layer;
    var laydate=layui.laydate;
    var layedit=layui.layedit;

    //日期时间选择器
    laydate.render({
        elem: '#startTime'
        ,type: 'datetime'
    });
    //日期时间选择器
    laydate.render({
        elem: '#endTime'
        ,type: 'datetime'
    });

    var tableIns = table.render({
        elem: '#classifyTable',
        url: '/admin/classify/list',
        toolbar: '#classifyToolBar',
        page: 'true',
        title: '分类数据表',
        cols: [ [
            {type:'checkbox'},
            {field:'id',title:'ID',align:'center'},
            {field:'categoryLevel',title:'分类级别',align:'center'},
            {field:'parentId',title:'父级ID',align:'center'},
            {field:'classifyName',title:'分类名',align:'center'},
            {field:'createTime',title:'创建时间',align:'center'},
            {field:'changeTime',title:'修改时间',align:'center'},
            {title:'操作',toolbar:'#classifyRowBar',width:250}
        ] ],
        done:function (res,curr,count) {
            if(curr>1 && res.data.length==0){
                tableIns.reload({
                    page:{curr:curr-1}
                });

            }
        }
    });



    //判断分类名是否重复
    var isClassifyName = false;
    $('input[name=classifyName]').blur(function() {
        var classifyName = $(this).val();
        var id = $('input[name=id]').val();
        $.get("/admin/classify/isClassifyNameCount",{"id":id,"classifyName":classifyName},function (res) {
            layer.msg(res.content);
            if(res.success){
                isClassifyName = true;
            }
        },"json");

    });


    form.on("submit(doSearch)",function (data) {
        tableIns.reload({
            url:'/admin/classify/list',
            where:data.field,
            page:{
                curr:1
            }
        })
        return false;
    })

    table.on('toolbar(classifyTable)',function (obj) {
        switch (obj.event) {
            case "add":
                openAddClassifyLayer();
                break;
            case "batchDelete":
                batchDelete();
                break;
        }
    })
    table.on('tool(classifyTable)',function (obj) {
        var data = obj.data;
        switch (obj.event) {
            case "update":
                openUpdateClassifyLayer(data);
                break;
            case "delete":
                deleteInfo(data);
                break;
            case "show":
                showClassify(data);
                break;
        }
    })

    //弹出层索引
    var mainIndex;
    //提交路径
    var url;

    //查看公告
    function  showClassify(data) {
        mainIndex = layer.open({
            type:1,
            content:$("#showClassifyDiv"),
            area:["300px","300px"],
            title:"查看分类",
            success:function () {
                $("#parent").html(data.title);
                $("#sublevel").html(data.classifyName);
                if(data.categoryLevel==2){
                    $.get("/admin/classify/selectClassifyName",{"parentId":data.parentId},function (res) {
                        if(res.success){
                            $("#parent").html(res.data.classifyName);
                        }else {
                            $("#parent").html("服务器异常");
                        }
                    },"json");
                }else{
                    $("#parent").html("没有上级了");
                }
            }
        });
    }

    //添加
    function openAddClassifyLayer() {
        mainIndex = layer.open({
            type:1,
            content:$("#addOrUpdateDiv"),
            area:["500px","400px"],
            title:"添加分类",
            success:function () {

                $("#dataFrm")[0].reset();
                url="/admin/classify/addClassify";

            }
        });

        //初始化富文本编辑器
        editIndex = layedit.build('content');
    }
    //修改
    function openUpdateClassifyLayer(data) {
        mainIndex = layer.open({
            type:1,
            content:$("#addOrUpdateDiv"),
            area:["500px","400px"],
            title:"修改分类",
            success:function () {
                form.val("dataFrm",data);
                url="/admin/classify/updateClassify";

            }
        });
    }
    //删除
    function deleteInfo(data){
        layer.confirm("确定要删除这条数据吗？",{icon:3,title:"提示"},function (index) {
            $.post("/admin/classify/deleteClassify",{"id":data.id},function (result) {
                if(result.success){
                    tableIns.reload();
                }
                layer.msg(result.content);
            },"json")
            //关闭弹窗
            layer.close(index);
        })
    }

    //批量删除
    function batchDelete(){
        //获取选中的行
        var checkStatus = table.checkStatus("classifyTable");
        //判断是否选中行
        var length = checkStatus.data.length;
        if(length>0){
            layer.confirm('确定要删除这些数据吗?',{icon:3,title:"提示"},function (index) {
                var data = checkStatus.data;
                var idArr = []
                for(var i = 0 ; i<length;i++){
                    idArr.push(data[i].id);
                }
                var ids = idArr.join(",");
                $.post("/admin/classify/batchDelete",{"ids":ids},function (result) {
                    if(result.success){
                        tableIns.reload();
                    }
                    layer.msg(result.content);
                },"json")
            })
            layer.close(index);
        }else{
            layer.alert("请选择要删除的行",{icon:0})
        }
    }

    //提交事件
    form.on("submit(doSubmit)",function (data) {

        if(isClassifyName){
            $.post(url,$("#dataFrm").serialize(),function (result) {
                if(result.success){
                    //提示信息
                    layer.msg(result.content,{icon:1});
                    //刷新数据表格
                    tableIns.reload();
                    //关闭窗口
                    layer.close(mainIndex);
                }else {
                    layer.msg(result.content,{icon:2});
                }
            },"json");
        }
        return false;
    })


});
